package com.sti.framework.models.User.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sti.framework.core.service.BaseService;
import com.sti.framework.core.web.Page;
import com.sti.framework.core.web.SimplePage;
import com.sti.framework.models.System.data.MenuManage;
import com.sti.framework.models.System.data.MenuRoleMngae;
import com.sti.framework.models.System.service.MenuRoleMngaeService;
import com.sti.framework.models.User.data.Authority;
import com.sti.framework.models.User.data.Role;
import com.sti.framework.models.User.data.RoleAuthority;
import com.sti.framework.models.User.mapper.RoleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleService extends BaseService<RoleMapper, Role> {

    @Inject
    private AuthorityService authorityService;
    @Inject
    private RoleAuthorityService roleAuthorityService;
    @Inject
    private MenuRoleMngaeService menuRoleMngaeService;

    public SimplePage<Role> getAll(Page page) {

        QueryWrapper<Role> wrapper = new QueryWrapper();
        wrapper.ne("id", 23691354597501L);
        IPage<Role> list = super.getBaseMapper().selectPage(page, wrapper);
        return new SimplePage(list.getRecords(), list.getTotal(),list.getCurrent(),list.getSize());
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveData(Role role) {
        this.create(role);
        //获取全选tag数据
        if(!CollectionUtils.isEmpty(role.getMenuManageList())){
            List<String> menuNameList = role.getMenuManageList().stream().map(MenuManage::getLabelName).collect(Collectors.toList());
            LambdaQueryWrapper<Authority> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Authority::getTable_name,menuNameList);
            List<Authority> authorityList = authorityService.list(lambdaQueryWrapper);
            List<RoleAuthority> roleAuthorityList = new ArrayList<>();
            //批量新增角色接口权限地址
            for (Authority authority : authorityList) {
                RoleAuthority roleAuthority = new RoleAuthority();
                roleAuthority.setRole_id(role.getId());
                roleAuthority.setData_level(1);
                roleAuthority.setTag(authority.getTag());
                roleAuthority.setAccess_level(1);
                roleAuthorityList.add(roleAuthority);
            }
            roleAuthorityService.saveBatch(roleAuthorityList);
            //记录角色与菜单的关系
            List<MenuRoleMngae> menuRoleMngaeList = new ArrayList<>();
            for (MenuManage menuManage : role.getMenuManageList()) {
                MenuRoleMngae menuRoleMngae = new MenuRoleMngae();
                menuRoleMngae.setRoleId(role.getId());
                menuRoleMngae.setMenuId(menuManage.getId());
                menuRoleMngaeList.add(menuRoleMngae);
            }
            menuRoleMngaeService.saveBatch(menuRoleMngaeList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateData(Role role) {
        this.updateById(role);
        //删除角色接口权限数据
        roleAuthorityService.deleteForRole(role.getId());
        //删除vue菜单管理数据
        QueryWrapper<MenuRoleMngae> wrapper = new QueryWrapper<>();
        wrapper.eq("roleId",role.getId());
        menuRoleMngaeService.remove(wrapper);
        //获取全选tag数据
        if(!CollectionUtils.isEmpty(role.getMenuManageList())){
            List<String> menuNameList = role.getMenuManageList().stream().map(MenuManage::getLabelName).collect(Collectors.toList());
            LambdaQueryWrapper<Authority> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.in(Authority::getTable_name,menuNameList);
            List<Authority> authorityList = authorityService.list(lambdaQueryWrapper);
            List<RoleAuthority> roleAuthorityList = new ArrayList<>();
            //批量新增角色接口权限地址
            for (Authority authority : authorityList) {
                RoleAuthority roleAuthority = new RoleAuthority();
                roleAuthority.setRole_id(role.getId());
                roleAuthority.setData_level(1);
                roleAuthority.setTag(authority.getTag());
                roleAuthority.setAccess_level(1);
                roleAuthorityList.add(roleAuthority);
            }
            roleAuthorityService.saveBatch(roleAuthorityList);
            //记录角色与菜单的关系
            List<MenuRoleMngae> menuRoleMngaeList = new ArrayList<>();
            for (MenuManage menuManage : role.getMenuManageList()) {
                MenuRoleMngae menuRoleMngae = new MenuRoleMngae();
                menuRoleMngae.setRoleId(role.getId());
                menuRoleMngae.setMenuId(menuManage.getId());
                menuRoleMngaeList.add(menuRoleMngae);
            }
            menuRoleMngaeService.saveBatch(menuRoleMngaeList);
        }
    }
}
